//
//  Public_View.m
//  HCFrame
//
//  Created by 丁 一 on 2018/4/23.
//  Copyright © 2018年 丁 一. All rights reserved.
//

#import "Start_AddView.h"

@interface Start_AddView (){
    NSString *Img_Url;
}

@property (nonatomic,strong) UIImageView   *imgview;
@property (nonatomic,strong) UIButton      *BG_Button;
@property (nonatomic,strong) UIButton      *TG_Button;
@property (nonatomic,strong) UILabel       *Tixing;

@end

@implementation Start_AddView

-(instancetype)init:(NSString *)apiurl{
    if (self=[super init]) {
        Img_Url = apiurl;
        
        [self overapixian];
        
    }
    return self;
}

-(void)overapixian{
    self.backgroundColor=[UIColor whiteColor];
    self.frame = [[UIScreen mainScreen] bounds];
    
    _imgview   = [[UIImageView alloc] initWithFrame:self.frame];
    _imgview.contentMode = UIViewContentModeScaleAspectFill;
    
    BOOL cun_lin = NO;
    NSURL *url = [NSURL URLWithString:Img_Url];
    SDWebImageManager *manager = [SDWebImageManager sharedManager];
    [manager diskImageExistsForURL:url];
    if ([manager diskImageExistsForURL:url]) {
        cun_lin = YES;
    }else{
        cun_lin = NO;
    }

    [_imgview sd_setImageWithURL:[NSURL URLWithString:Img_Url] placeholderImage:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        
        if (image&&cun_lin) {
            [self setimg:image];
        }
        
    }];
    
}

- (void)setimg:(UIImage *)addimg{
    [self addSubview:_imgview];
    
    _BG_Button = [UIButton buttonWithType:UIButtonTypeCustom];
    _BG_Button.frame=CGRectMake(0, 100, RECRWITCH, RECERHEIGHT-100);
    [_BG_Button setBackgroundColor:[UIColor clearColor]];
    [_BG_Button addTarget:self action:@selector(changetop)  forControlEvents:UIControlEventTouchUpInside];
    
    _TG_Button = [UIButton buttonWithType:UIButtonTypeCustom];
    _TG_Button.frame=CGRectMake(RECRWITCH-55, 24, 45, 45);
    [_TG_Button addTarget:self action:@selector(skipto)  forControlEvents:UIControlEventTouchUpInside];
    _TG_Button.layer.masksToBounds=YES;
    _TG_Button.layer.cornerRadius=22.5;
    _TG_Button.layer.borderColor=[UIColor blackColor].CGColor;
    _TG_Button.layer.borderWidth=1.0;
    [_TG_Button setBackgroundColor:[UIColor clearColor]];
    [_TG_Button setTitle:[NSString stringWithFormat:@"跳过4s"] forState:UIControlStateNormal];
    [_TG_Button.titleLabel setFont:[UIFont systemFontOfSize:12]];
    
    
    __block int timeout=(5-1);
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
    dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1*NSEC_PER_SEC, 0);
    dispatch_source_set_event_handler(_timer, ^{
        if(timeout<0){
            dispatch_source_cancel(_timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                self->_TG_Button.frame=CGRectMake(RECRWITCH-55, -RECRWITCH, 45, 45);
            });
        }else{
            int seconds = timeout % 90;
            NSString *strTime = [NSString stringWithFormat:@"%d", seconds];
            dispatch_async(dispatch_get_main_queue(), ^{
                [self->_TG_Button setTitle:[NSString stringWithFormat:@"跳过%@s",strTime] forState:UIControlStateNormal];
                
            });
            timeout--;
        }
    });
    dispatch_resume(_timer);
    
    UIWindow *mainWindow = [UIApplication sharedApplication].keyWindow;
    [mainWindow addSubview:self];
    [mainWindow addSubview:_BG_Button];
    [mainWindow addSubview:_TG_Button];
    
    [UIView animateWithDuration:0.5f delay:4.6 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        //self.alpha = 0.0f;
        self.frame=CGRectMake(0, -RECERHEIGHT-200, RECRWITCH, RECERHEIGHT);
    } completion:^(BOOL finished) {
        [self       removeFromSuperview];
        [self->_BG_Button removeFromSuperview];
        [self->_TG_Button removeFromSuperview];
    }];
}

-(void)changetop{
    [self skipto];
    self.seletBlock(@"xx");
}

-(void)skipto{
    [self       removeFromSuperview];
    [_BG_Button removeFromSuperview];
    [_TG_Button removeFromSuperview];
}

@end
